<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\user;


use app\common\lib\Common;
use app\common\lib\Guzzle;
use app\common\lib\Spreadsheet;
use app\exception\IndexException;
use app\logic\BaseLogic;
use app\model\novel\NovelSpreadModel;
use app\model\platform\PlatformManageModel;

class PlatformAccountLogic extends BaseLogic
{
    protected $model;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->model = \app\model\user\UserPlatformAccountModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function getList($param)
    {
        $where = [
            'isdel'=>0,
            'user_sn'=>request()->curUserId,
        ];

        $fields = ['id','user_sn','platform_id','platform_name','logo','nickname','account_id'];
        $order = ['created_at', 'desc'];

        $list =  $this->model->index($where, $fields, $order);
        return $list;
    }

    public function submitAccount($param)
    {
        if(Common::setnx('submitAccount_spread_'.request()->curUserId)==false){
            throw new IndexException(4020,'请不要频繁提交');
        }

        $platform = PlatformManageModel::getInstance()->getWhere(['id'=>$param['platform_id']])->first();
        if($platform==false){
            throw new IndexException(4020,'平台不存在');
        }

        $where = [
                'user_sn'=>request()->curUserId,
                'platform_name'=>$platform['name'],
                'account_id'=>$param['account_id'],
                'isdel'=>0,
        ];
        $count = $this->model->getWhere($where)->count();
        if($count){
            throw new IndexException(4020,'该平台账号已添加');
        }
        $insert = [
            'user_sn'=>request()->curUserId,
            'platform_id'=>$param['platform_id'],
            'platform_name'=>$platform['name']??'',
            'logo'=>$platform['logo'],
            'nickname'=>$param['nickname'],
            'account_id'=>$param['account_id'],
        ];
        $id = $this->model->insertGetId($insert);

        return $id;
    }
}